<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $shipp_model = D('Shipp');
        $count = $shipp_model->getShippCount();
        $this->assign('count',$count);
        $this->display('index2');
    }

    public function ration(){
        if(IS_FILE){
            //$shipp_model = D('Shipp');
            $shipp_model = D('Shipp');
            $upload = new \Think\Upload();
            $upload->maxSize = 3145728;
            $upload->exts = array('txt');
            $upload->rootPath = './Uploads/';
            $upload->savePath = 'txt';
            $info = $upload->upload();
            $save_path = './Uploads/'.$info['file_data']['savepath'].$info['file_data']['savename'];
            $file_content = file_get_contents($save_path);
            $data = str_replace("\n",',',trim($file_content));
            $shipp_list = $shipp_model->getShippList(array('shipp_sn'=>array(array('in',$data))));
            if(!$shipp_list){
                $arr = $this->_getArrayFor2D(explode(',',$data),'shipp_sn');
                $ins_row = $shipp_model->saveShippAll($arr);
                echo $shipp_model->getLastSql();
                if($ins_row) return 1;
            }
            $str = $this->_getStringForArr($shipp_list,'shipp_sn');
            echo "以下运单号重复\n".$str;
            $insert_str = $this->_delRepeat($data,explode("\n",$str));
            $shipp_data = $this->_getArrayFor2D(explode(',',$insert_str),'shipp_sn');
            $shipp_model->saveShippAll($shipp_data);
        }
    }
    
    private function _getArrayFor2D($data,$key){
        foreach($data as $col=>$item){
            if(!empty($item)){
                $redata[][$key] = strip_tags(trim($item));
            }
        }
        return $redata;
    }

    private function _getStringForArr($data,$key){
        $str = '';
        foreach($data as $col=>$item){
            $str.=$item[$key]."\n";
        }
        return $str;
    }

    private function _delRepeat($str,$rep){
        foreach($rep as $col=>$item){
            $str = str_replace($item,'',$str);
        }
        return $str;
    }
}
